package com.wangzeqi.neihanduanzi.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.wangzeqi.neihanduanzi.base.BaseActivity;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.discovery.DiscoveryActivity;

import com.wangzeqi.neihanduanzi.enter.EnterPersonActivity;
import com.wangzeqi.neihanduanzi.home.circle.FragmentCircle;
import com.wangzeqi.neihanduanzi.home.city.FragmentCity;
import com.wangzeqi.neihanduanzi.home.essence.FragmentEssence;
import com.wangzeqi.neihanduanzi.home.jokes.FragmentJokes;
import com.wangzeqi.neihanduanzi.home.picture.FragmentPicture;
import com.wangzeqi.neihanduanzi.home.recommend.FragmentRecommend;
import com.wangzeqi.neihanduanzi.home.subscription.FragmentSubscription;
import com.wangzeqi.neihanduanzi.home.video.FragmentVideo;
import com.wangzeqi.neihanduanzi.msg.MsgActivity;
import com.wangzeqi.neihanduanzi.news.activity.NewsActivity;

import java.util.ArrayList;


/**
 * Created by dllo on 16/1/7.
 */
public class HomeActivity extends BaseActivity implements View.OnClickListener {

    private ArrayList<Fragment> data;
    private HomeAdapter adapter;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ImageButton imageButton;
    private ImageButton imageButtonPerson;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        findViewById(R.id.rl_home_btn).setOnClickListener(this);
        findViewById(R.id.rl_discovery_btn).setOnClickListener(this);
        findViewById(R.id.rl_review_btn).setOnClickListener(this);
        findViewById(R.id.rl_msg_btn).setOnClickListener(this);
        imageButton= (ImageButton) findViewById(R.id.scroll_btn);
        imageButton.setOnClickListener(this);
        imageButtonPerson = (ImageButton) findViewById(R.id.home_top_left_ib);
        imageButtonPerson.setOnClickListener(this);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (TabLayout) findViewById(R.id.tab_top);
        data= new ArrayList<>();
        data.add(new FragmentRecommend());
        data.add(new FragmentVideo());
        data.add(new FragmentPicture());
        data.add(new FragmentJokes());
        data.add(new FragmentSubscription());
        data.add(new FragmentEssence());
        data.add(new FragmentCity());
        data.add(new FragmentCircle());
        adapter = new HomeAdapter(getSupportFragmentManager(),data,this);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setHorizontalScrollBarEnabled(true);
        tabLayout.setSelectedTabIndicatorColor(Color.HSVToColor(100, new float[]{200, 0, 0}));
        tabLayout.setSelectedTabIndicatorHeight(120);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);



        for (int  i= 0 ; i<tabLayout.getTabCount();i++){

            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null){
                tab.setCustomView(adapter.getTabView(i));
            }

        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.scroll_btn:
          tabLayout.setScrollPosition(7,0,false);

                break;
            case R.id.rl_home_btn:
                Intent intentHome = new Intent(this, HomeActivity.class);
                startActivity(intentHome);
                break;
            case R.id.rl_discovery_btn:
                Intent intentDiscovery = new Intent(this, DiscoveryActivity.class);
                startActivity(intentDiscovery);
                finish();
                break;
            case R.id.rl_review_btn:
                Intent intentReview = new Intent(this, NewsActivity.class);
                startActivity(intentReview);
                finish();
                break;
            case R.id.rl_msg_btn:
                Intent intentMsg = new Intent(this, MsgActivity.class);
                startActivity(intentMsg);
                finish();
                break;
            case R.id.home_top_left_ib:
                Intent intent = new Intent(this, EnterPersonActivity.class);
                startActivity(intent);
                break;


        }
    }
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
